<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">
              {{this.$store.state.city}}
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" @click="changeCity(item.name)" v-for="item in hotCities" :key="item.id">
            <div class="button" v-text="item.name">
            </div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list" v-for="cityGroup in item" @click="changeCity(cityGroup.name)" :key="cityGroup.id">
          <div class="item border-bottom" v-text="cityGroup.name"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    changeCity (c) {
      this.$store.dispatch('switchCity', c)
      this.$router.push('/')
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const ele = this.$refs[this.letter][0]
        this.scroll.scrollToElement(ele)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  .border-bottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    left: 0
    top: 1.58rem
    right: 0
    bottom: 0
    .title
      font-size: .26rem
      color: #666
      background: #eee
      padding-left: .2rem
      line-height: .4rem
    .button-list
      padding: .1rem .6rem .1rem .1rem
      overflow: hidden
      .button-wrapper
        float: left
        width: 33.3%
        .button
          border: .02rem solid #ccc
          margin: .1rem
          padding: .1rem 0
          text-align: center
    .item-list
      padding:
      .item
        padding-left: .2rem
        line-height: .64rem
</style>
